import DarkLayer from "./DarkLayer";
import { Logger } from "../utils/Logger";

const { ccclass } = cc._decorator;

@ccclass
export default class DialogBase extends cc.Component {

    private static LocalZOrder: number = 5;


    private darkLayer: cc.Node = null;
    private static showingNameList: Array<string> = []

    onLoad() {
        DialogBase.LocalZOrder += 1;
        if (DialogBase.showingNameList.indexOf(this.name) != -1) {
            this.node.destroy();
        } else {
            DialogBase.showingNameList.push(this.name);
        }
        // Logger.log("showingNameList===", this.name);
        let closeLayer:cc.Node = this.node.getChildByName("closeLayer")
        if(closeLayer){
            let closeLayerWidget:cc.Widget = closeLayer.getComponent(cc.Widget);
            if(closeLayerWidget){
                closeLayerWidget.target = cc.Canvas.instance.node;
                closeLayerWidget.left = 0;
                closeLayerWidget.right = 0;
                closeLayerWidget.top = 0;
                closeLayerWidget.bottom = 0;
            }
        }
        this.onLoadMe();
    }

    onLoadMe() {

    }

    start(isPlayMv: boolean = false) {
        this.darkLayer = DarkLayer.getDarkLayer();
        this.node.addChild(this.darkLayer, -1);
        if (isPlayMv) {
            this.node.setScale(0, 0);
            let seq = cc.sequence(cc.scaleTo(0.3, 1, 1).easing(cc.easeExponentialInOut()), cc.callFunc(() => {
                this.onStartMe();
            }));
            this.node.runAction(seq);
        } else {
            this.onStartMe();
        }
    }

    onStartMe() {

    }

    onClickClose() {
        this.node.destroy();
    }

    update(dt) {
        this.onUpdateMe(dt);
    }

    onUpdateMe(dt) {

    }

    onDestroy() {
        DialogBase.LocalZOrder -= 1;
        if (this.darkLayer) {
            this.darkLayer.destroy();
        }
        let index: number = DialogBase.showingNameList.indexOf(this.name);
        DialogBase.showingNameList.splice(index, 1);
        this.onDestroyMe();
    }

    onDestroyMe() {

    }
}
